let com_num = 0;
let timer = '';
let is_ok = false;

Page({
  /**
   * 页面的初始数据
   */
  data: {
    computerUrl: '',
    userUrl: '/pages/images/user.jpg',
    srcs: [
      '/pages/images/shitou.jpg',
      '/pages/images/jiandao.jpg',
      '/pages/images/bu.jpg'
    ],
    result_content: '',
    win_num: 0
  },

  move() {
    if (com_num >= 3) {
      com_num = 0;
    }
    this.setData({ computerUrl: this.data.srcs[com_num] });
    com_num++;

  },

  timerGo() {
    timer = setInterval(this.move, 100);
  },

  fn_change(e) {
    if (is_ok) {
      return;
    }
    this.setData({ userUrl: this.data.srcs[e.target.id] });
    clearInterval(timer);
    is_ok = true;

    let computer_url = this.data.computerUrl;
    let user_rl = this.data.userUrl;
    let winnum = this.data.win_num;
    if (user_rl == '/pages/images/shitou.jpg' && computer_url == '/pages/images/jiandao.jpg') {
      this.setData({ result_content: '你赢了' });
      winnum++;
      this.data.win_num = winnum;
      this.setData({ win_num: winnum });
    } else if (user_rl == '/pages/images/jiandao.jpg' && computer_url == '/pages/images/bu.jpg') {
      this.setData({ result_content: '你赢了' });
      winnum++;
      this.data.win_num = winnum;
      this.setData({ win_num: winnum });
    } else if (user_rl == '/pages/images/bu.jpg' && computer_url == '/pages/images/shitou.jpg') {
      this.setData({ result_content: '你赢了' });
      winnum++;
      this.data.win_num = winnum;
      this.setData({ win_num: winnum });
    } else if (user_rl == '/pages/images/shitou.jpg' && computer_url == '/pages/images/shitou.jpg') {
      this.setData({ result_content: '平局' });
    } else if (user_rl == '/pages/images/jiandao.jpg' && computer_url == '/pages/images/jiandao.jpg') {
      this.setData({ result_content: '平局' });
    } else if (user_rl == '/pages/images/bu.jpg' && computer_url == '/pages/images/bu.jpg') {
      this.setData({ result_content: '平局' });
    } else {
      this.setData({ result_content: '你输了' });
    }
  },

  again_btn() {
    clearInterval(timer);
    timer = setInterval(this.move, 100);
    this.setData({ userUrl: '/pages/images/user.jpg' });
    is_ok = false;
    this.setData({ result_content: '' });
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.timerGo();
  },

  onShareAppMessage() {
    return {
      title: '猜拳',
      path: page/index/index
    }
  }

})